import { ref, computed } from 'vue';
import { formatDate } from 'pub-bbx-utils';
import platform, { isOpenData } from './platform';
import { t } from '@src/locales';
import { getRootWindow } from '@src/util/dom';
/**
 * @description 获取表格排序最小列宽
 * @param {Object} field 字段对象，目前主要确保有displayName
 * @param {Number} fontSize 字体大小
 * @param {Number} padding 单元格左右padding和
 * @param {Number} iconWidth 排序图标的占用宽度
 */
export function getSortableMinWidth(field: any, fontSize = 14, padding = 20, iconWidth = 24) {
  return field.displayName.length * (fontSize + 2) + padding + iconWidth;
}

export const defaultTableData: { defaultPageSizes: number[]; defaultLayout: string } = {
  defaultPageSizes: [10, 20, 30, 40, 50, 100],
  defaultLayout: 'total, prev, pager, next, sizes, jumper',
};

export const useTabelExportNormalDataHook = ({ exportFileName = '', pageFnc = null, searchParamsFnc = null, exportHttp = null, multipleSelectionFnc, BbxExportStorageKey, exportColumnsFnc, filedListParamsFnc = null }: any) => {
  const selectedIds = computed(()=>multipleSelectionFnc().map(i=>i.id));
  function BbxExportProduct(exportAll: boolean, exportModalDom: any) {
    let ids: Array<any> = [];
    const fileName = `${formatDate(new Date(), 'YYYY-MM-DD')}${exportFileName}.xlsx`;
    const multipleSelection: any = multipleSelectionFnc();
    if (!exportAll) {
      if (!multipleSelection.length) return platform.alert(t('common.base.tip.exportUnChooseTip'));
      ids = selectedIds.value;
    }else{
      ids = []
    }
    exportModalDom.open(ids, fileName);
  }
  function BbxExportAlert(result: any, params = {}) {
    platform.alert(result.message);
  }

  /**
   * 导出数据
   */
  function BbxExportData(number, list = [], type?: any = null) {
    const export_list = exportColumnsFnc();
    console.log(export_list, 'export_list')
    if (!list.length) return [];
    if (number === 3) {
      const cardField = [];
      export_list
        .filter((item, index) => {
          return type === 'cardFieldChecked' ? item.value !== 'productExport' : index > 2;
        })
        .forEach(v => {
          v.columns.forEach(item => {
            cardField.push(item);
          });
        });
      return cardField
        .map(v => {
          const bool = list.some(item => {
            if (v.exportAlias) {
              return v.exportAlias === item;
            }
            return v.fieldName === item;
          });
          if (bool) {
            return v.exportAlias ? v.exportAlias : v.fieldName;
          }
        })
        .filter(item => {
          return item;
        });
    }

    return export_list[number].columns
      .map(v => {
        const bool = list.some(item => {
          if (v.exportAlias) {
            return v.exportAlias === item;
          }
          return v.fieldName === item;
        });
        if (bool) {
          return v.exportAlias ? v.exportAlias : v.fieldName;
        }
      })
      .filter(item => {
        return item;
      });
  }

  /**
   * @description 构建导出参数
   * @return {Object} 导出参数
   */
  function BbxBuildExportParams(checkedMap, ids, exportOneRow) {

    return filedListParamsFnc(checkedMap, ids, exportOneRow);
  }

  const BbxExportHttpFnc = exportHttp;

  return {
    BbxExportHttpFnc,
    isOpenData,
    BbxBuildExportParams,
    BbxExportData,
    BbxExportProduct,
    BbxExportAlert,
    BbxExportStorageKey,
  };
};
